package Map集合;

import java.util.HashMap;
import java.util.Set;
/*
* 键是学生对象，值是string类型的数据
* */
public class HashMap集合练习2 {
    public static void main(String[] args) {
        /*创建HashMap集合对象*/
        HashMap<student, String> hm = new HashMap<>();
        /*创建学生对象*/
        student s1 = new student("林青霞", 30);
        student s2 = new student("张曼玉", 35);
        student s3 = new student("王祖贤", 33);
        student s4 = new student("王祖贤", 33);
        /*把学生添加到集合*/
        hm.put(s1,"西安");
        hm.put(s2,"武汉");
        hm.put(s3,"郑州");
        hm.put(s4,"北京");
        /*遍历集合*/
        Set<student> keySet = hm.keySet();
        for (student key:keySet) {
            String value = hm.get(key);
            System.out.println(key.getName()+","+key.getAge()+","+value);
        }
    }
}
/*
* 学生类
* */
class student{
    private String name;
    private int age;

    public student() {
    }

    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean equals(Object o){
        if(this==o){
            return true;
        }
        if(o==null||getClass()!=o.getClass()){
            return false;
        }
        student student = (student) o;
        if(age!=student.age){
            return false;
        }
        return name!=null?name.equals(student.name):student.name==null;
    }

    public int hashCode(){
        int result=name!=null?name.hashCode():0;
        result=31*result+age;
        return result;
    }

}















